package com.crm.common.model.Enum;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.beust.jcommander.internal.Lists;
import com.crm.common.util.DateUtil;
import com.crm.common.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description: 统计周期
 * @author: lixq-f
 * @create: 2021/04/17
 */
@Getter
@AllArgsConstructor
public enum StatisticalPeriodEnum implements Serializable {


    THIS_WEEK(1, "本周"),
    THIS_MONTH(2, "本月"),
    LAST_MONTH(3, "上月"),
    RECENTLY_THREE_MONTH(4, "近三月"),
    THIS_QUARTER(5, "本季度"),
    THIS_YEAR(6, "本年度"),
    ;

    private int code;
    private String desc;


    public static boolean containPeriod(Integer period) {
        if (Objects.isNull(period)) {
            return Boolean.FALSE;
        }
        List<Integer> list = Lists.newArrayList();
        for (StatisticalPeriodEnum value : StatisticalPeriodEnum.values()) {
            list.add(value.code);
        }
        return list.contains(period);
    }


    public static Date getStartDateByPeriod(int dateType) {
        switch (dateType) {
            case 1:
                return DateUtil.getTimesWeekMorning();
            case 2:
                return DateUtil.getTimesMonthMorning();
            case 3:
                DateTime dateTime = DateUtils.lastMonth();
                return DateUtils.beginOfMonth(dateTime);
            case 6:
                return DateUtils.beginOfYear(new Date());
            default:
                return DateUtil.getTimesWeekMorning();
        }
    }



    public static Date getEndDateByPeriod(int dateType) {
        switch (dateType) {
            case 1:
                return DateUtil.getSysDate();
            case 2:
                return DateUtil.getSysDate();
            case 3:
                DateTime dateTime = DateUtils.lastMonth();
                return DateUtils.endOfMonth(dateTime);
            case 6:
                return DateUtil.getSysDate();
            default:
                return DateUtil.getSysDate();
        }
    }



    /**
     * 获取初始化时间- yyyy-MM-dd
     *
     * @param dateType
     * @return
     */
    public static List<String> getInitTime(int dateType) {
        Date startDate = null;
        Date nowDate = DateUtil.getSysDate();
        if (StatisticalPeriodEnum.THIS_WEEK.getCode() == dateType) {
            startDate = DateUtil.getTimesWeekMorning();
            return DateUtils.getBetweenDateList(startDate, nowDate, DateUtils.DATE_FORMAT_YYYY_MM_DD, 1);
        }
        if (StatisticalPeriodEnum.THIS_MONTH.getCode() == dateType) {
            startDate = DateUtil.getTimesMonthMorning();
            return DateUtils.getBetweenDateList(startDate, nowDate, DateUtils.DATE_FORMAT_YYYY_MM_DD, 1);
        }
        if (StatisticalPeriodEnum.LAST_MONTH.getCode() == dateType) {
            DateTime dateTime = DateUtils.lastMonth();
            DateTime beginOfLastMonth = DateUtils.beginOfMonth(dateTime);
            DateTime endOfLastMonth = DateUtils.endOfMonth(dateTime);
            return DateUtils.getBetweenDateList(beginOfLastMonth, endOfLastMonth, DateUtils.DATE_FORMAT_YYYY_MM_DD, 1);
        }
        if (StatisticalPeriodEnum.RECENTLY_THREE_MONTH.getCode() == dateType) {
            DateTime dateTime = DateUtils.offsetMonth(DateUtil.getSysDate(), -2);
            DateTime startTime = DateUtils.beginOfMonth(dateTime);
            Date endTime = DateUtil.getSysDate();
            return DateUtils.getBetweenDateList(startTime, endTime, DateUtils.DATE_FORMAT_YYYY_MM_DD, 1);

        }
        if (StatisticalPeriodEnum.THIS_QUARTER.getCode() == dateType) {
            DateTime beginOfQuarter = DateUtils.beginOfQuarter(DateUtil.getSysDate());
            Date endOfQuarter = DateUtil.getSysDate();
            return DateUtils.getBetweenDateList(beginOfQuarter, endOfQuarter, DateUtils.DATE_FORMAT_YYYY_MM_DD, 1);

        }
        if (StatisticalPeriodEnum.THIS_YEAR.getCode() == dateType) {
            DateTime dateTime = DateUtils.beginOfYear(DateUtil.getSysDate());
            List<DateTime> dateTimes = DateUtils.rangeToList(dateTime, DateUtil.getSysDate(), DateField.MONTH);
            return dateTimes.stream().map(date -> DateUtil.formatDate(date, DateUtils.DATE_FORMAT_YYYY_MM))
                    .collect(Collectors.toList());
        }

        return null;
    }





}
